﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Drawing;

namespace subtitle_modifier
{
    class GoodLangFile :SrtFile
    {
        protected static readonly ILog log = LogManager.GetLogger(typeof(GoodLangFile));
        public GoodLangFile(string fullPath): base(fullPath){}


        public int timePointCheker(SrtFile srtFile, TimeSpan diff, int numberOfTest)
        {
            int result = 0;
            Random random = new Random();
            Form1.Instance.addInfo("0% complited");
            for (int i = 0; i <= numberOfTest; i++)
            {
                Record recordToTest = this.GetRandomRecord();
                if (!recordToTest.Formated)
                {
                    bool isThere = isRecordTimeInWithDiff(recordToTest, diff);
                    Form1.Instance.replaceLastLine(String.Format("{0}% complited", (100 * i / numberOfTest)));
                    if (isThere)
                    {
                        result = result + 1;
                    }
                }
            }

          return (100*result)/numberOfTest;
        }

        private bool isRecordTimeInWithDiff(Record r, TimeSpan diff)
        {
            bool isThere = false;
            TimeSpan middelTime = getMiddleTimePlusDiff(r.StartShowingTime, r.EndShowingTime, diff);
           int place=this.OnWhichRecordDoesTextdisplayed(middelTime);
           if (place > -1)
            {
                isThere = true;
            }

            return isThere;
        }

        private TimeSpan getMiddleTimePlusDiff(TimeSpan startTimeSpan, TimeSpan endTimeSpan, TimeSpan diff)
        {
            Double startTimeSpanMilliseconds = startTimeSpan.Ticks;
            Double endTimeSpanMilliseconds = endTimeSpan.Ticks;
            
            long middleTimeSpanMilliseconds = (long)((endTimeSpanMilliseconds + startTimeSpanMilliseconds)/2)+diff.Ticks;

            TimeSpan timeSpan = new TimeSpan(middleTimeSpanMilliseconds);
            return timeSpan;
        }

        public bool SyncTimeWithExternalSrtFile(SrtFile ExternalSrtFile)
        {

            int res = 0;
            int count = 0;
            int tryNumber = 4;//from xml
            TimeSpan DiffPhase1;
            do
            {
                count++;
                if (count == tryNumber)
                {
                    log.Info("Sync Faild due to too many attempts");

                    Form1.Instance.addInfo("Sync Faild due to too many attempts",Color.Red);
                    System.Media.SystemSounds.Hand.Play();
                    
                    return false;
                }
                log.Info("Attempt #" + count + " to sync times of" + this.Info.Name + "with " + ExternalSrtFile.Info.Name);
                Form1.Instance.addInfo("Attempt #" + count +"/"+(tryNumber-1)+" to sync times of " + this.Info.Name + " with " + ExternalSrtFile.Info.Name);
                Form1.Instance.addInfo("Starting phase 1/2.");
                DiffPhase1 = this.GetDiffFromFilePhase1(ExternalSrtFile);
                Form1.Instance.addInfo("Starting phase 2/2.");
                res = this.timePointCheker(ExternalSrtFile, DiffPhase1, 100);
            } while ( res<95 );//from XML
            
            this.AddTimeToRecords(DiffPhase1);

            log.Info("Sync was successful!");
            return true;
        }
    }
}
